import Vue from 'vue'
import Vuex from 'vuex'

import {
  sysClassifyQuery,
  sysClassifyDelete,
  sysClassifyAdd,
  sysClassifyUpdate,
  sysClassifyOpenApiQuery,
} from '@/api/sysClassify'

Vue.use(Vuex)

const state = {
  tableData: [],
  allNavList: [],
}

const getters = {}

const actions = {
  //分类增删改查
  async sysClassifyList({ commit }, listQuery) {
    let res = await sysClassifyQuery({ ...listQuery })
    commit('SET_CLASSIFYDATA', res.data.data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async openApiQuerySysClassify({ commit }, listQuery) {
    let res = await sysClassifyOpenApiQuery({ ...listQuery })
    commit('SET_CLASSIFYDATA', res.data.data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },

  async sysClassifyDel({ commit }, row) {
    let res = await sysClassifyDelete(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async sysClassifyADD({ commit }, data) {
    let res = await sysClassifyAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async sysClassifyUPDATE({ commit }, data) {
    let res = await sysClassifyUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },

  async queryAllNavList({ commit, dispatch, state }, classType) {
    if (state.allNavList.length > 0) return state.allNavList
    let obj = {
      page: 1,
      limit: 10,
      pid: 0,
      classType, //1.试卷 2.考试 3.栏目
    }
    let res = await dispatch('openApiQuerySysClassify', obj)
    commit('SET_ALLNAVLIST', res.data)
    return res.data
  },
}

const mutations = {
  SET_CLASSIFYDATA: (state, d) => {
    state.tableData = d
  },
  SET_ALLNAVLIST: (state, d) => {
    state.allNavList = d
  },
}

export default {
  state,
  getters,
  actions,
  mutations,
}
